from rest_framework import serializers

from goods.models import SKU



class SKUSerializer(serializers.ModelSerializer):
    """
    查询购物车勾选的序列化器
    """
    count = serializers.IntegerField(min_value=1, label='数量')


    class Meta:
        model = SKU
        fields = ('id','name','default_image_url','price','count')


class OrderSettlementSerialize(serializers.Serializer):
    """
    提交订单序列化器
    """
    # 运费  10.00  DecimalField 小数类型
    freight = serializers.DecimalField(label='运费',max_digits=10,decimal_places=2)
    skus =  SKUSerializer(many=True)


class GWCSelectSerialize(serializers.Serializer):
    """全选购物车数据"""
    selected = serializers.BooleanField(default=True, label='是否勾选')


class GWCDeleteSerializer(serializers.Serializer):
    """
    删除购物车数据
    """
    sku_id  =  serializers.IntegerField(min_value=1,label='sku id')
    def validated_sku_id(self, value):
        """
        验证sku_id 是否存在  value--
        :return:
        """
        try:
            SKU.objects.get(id=value)
        except:
            raise serializers.ValidationError('该商品不存在')

        return value


class GWCModelSerializer(serializers.ModelSerializer):
    """
    查询购物车序列化器
    """
    count = serializers.IntegerField(min_value=1, label='数量')
    # 勾选数据 要么勾选，要么没有勾选 default=True 设置为默认是勾选的
    selected = serializers.BooleanField(default=True, label='是否勾选')

    class Meta:
        model = SKU
        fields = ('id','name','default_image_url','price','comments','count','selected')


class GWCSerializers(serializers.Serializer):
    """添加购物车的"""
    sku_id  =  serializers.IntegerField(min_value=1,label='sku id')
    count =  serializers.IntegerField(min_value=1,label='数量')
    # 勾选数据 要么勾选，要么没有勾选 default=True 设置为默认是勾选的
    selected =  serializers.BooleanField(default=True,label='是否勾选')

    def validated_sku_id(self, value):
        """
        验证sku_id 是否存在  value--
        :return:
        """
        try:
            SKU.objects.get(id=value)
        except:
            raise serializers.ValidationError('该商品不存在')

        return value